﻿--[[	$Id: actionButtons.lua 2494 2012-09-10 06:18:44Z sdkyron@gmail.com $	]]

local _G = getfenv(0)

local pixelScale = caelLib.scale

local find = string.find

local color = RAID_CLASS_COLORS[caelLib.playerClass]

local hideCount = false
local hideHotkeys = true

local backdrop = {
	bgFile = caelMedia.files.buttonGloss,
	insets = {top = pixelScale(-1), left = pixelScale(-1), bottom = pixelScale(-1), right = pixelScale(-1)},
}

local StyleButton = function(self)

	if self.styled then
		return
	end

	local action = self.action
	local buttonName = self:GetName()

	if buttonName:match("MultiCast") or buttonName:match("ExtraActionButton") then
		return
	end

	local isPet			=	buttonName:find("PetActionButton")
	local isShapeshift	=	buttonName:find("StanceButton")

	local button		=	_G[buttonName]
	local name			=	_G[format("%sName", buttonName)]
	local icon			=	_G[format("%sIcon", buttonName)]
	local flash			=	_G[format("%sFlash", buttonName)]
	local floating		=	_G[format("%sFloatingBG", buttonName)]
	local count			=	_G[format("%sCount", buttonName)]
	local border		=	_G[format("%sBorder", buttonName)]
	local hotkey		=	_G[format("%sHotKey", buttonName)]
	local cooldown		=	_G[format("%sCooldown", buttonName)]
	local autocast		=	_G[format("%sAutoCastable", buttonName)]
	local texture		=	(isPet or isShapeshift) and _G[format("%sNormalTexture2", buttonName)] or _G[format("%sNormalTexture", buttonName)]

	if not texture then
		self.styled = true

		return
	end

	texture:SetAllPoints()
	texture:SetVertexColor(0.5, 0.5, 0.5, 1)

	icon:SetTexCoord(0.1, 0.9, 0.1, 0.9)
	icon:SetPoint("TOPLEFT", button, pixelScale(4.5), pixelScale(-4.5))
	icon:SetPoint("BOTTOMRIGHT", button, pixelScale(-4.5), pixelScale(4.5))

	button:SetNormalTexture(caelMedia.files.buttonNormal)

	button.pushed = button:CreateTexture("Frame", nil, self)
	button.pushed:SetTexture(0.84, 0.75, 0.65, 0.75)
	button.pushed:SetAllPoints(icon)

	button.checked = button:CreateTexture("Frame", nil, self)
	button.checked:SetTexture(0.33, 0.59, 0.33, 0.5)
	button.checked:SetAllPoints(icon)

	button.highlight = button:CreateTexture("Frame", nil, self)
	button.highlight:SetTexture(color.r, color.g, color.b, 0.5)
	button.highlight:SetAllPoints(icon)

	button:SetPushedTexture(button.pushed)
	button:SetCheckedTexture(button.checked)
	button:SetHighlightTexture(button.highlight)

	border:SetPoint("TOPLEFT", button, pixelScale(-1), pixelScale(1))
	border:SetPoint("BOTTOMRIGHT", button, pixelScale(1), pixelScale(-1))
	border:SetTexture(caelMedia.files.buttonNormal)

	flash:SetTexture(caelMedia.files.buttonFlash)

	if autocast then
		autocast:SetTexCoord(0.24, 0.75, 0.24, 0.75)
		autocast:SetAllPoints()
	end

	cooldown:SetParent(button)
	cooldown:ClearAllPoints()
	cooldown:SetAllPoints(icon)

	button.backdrop = CreateFrame("Frame", nil, button)
	button.backdrop:SetPoint("TOPLEFT", pixelScale(-1), pixelScale(1))
	button.backdrop:SetPoint("BOTTOMRIGHT", pixelScale(1), pixelScale(-1))
	button.backdrop:SetBackdrop(caelMedia.borderTable)
	button.backdrop:SetBackdropBorderColor(0, 0, 0)

	button.gloss = CreateFrame("Frame", nil, button)
	button.gloss:SetAllPoints()
	button.gloss:SetBackdrop(backdrop)
	button.gloss:SetBackdropColor(0.25, 0.25, 0.25, 0.5)

	if not hideCount then
		count:SetParent(button.gloss)
		count:SetFont(caelMedia.fonts.NORMAL, 12, "OUTLINEMONOCHROME")
		count:SetPoint("BOTTOMRIGHT", pixelScale(3), pixelScale(-1))
	else
		count:Hide()
	end

	hotkey:SetParent(button.gloss)
	hotkey:SetFont(caelMedia.fonts.NORMAL, 12, "OUTLINEMONOCHROME")
	hotkey:ClearAllPoints()
	hotkey:SetPoint("TOPRIGHT", pixelScale(3), pixelScale(1))

	if hideHotkeys then
		hotkey:Hide()
	end

	if name then
		name:Hide()
	end

	if floating then
		floating:Hide()
	end
--[[
	if not button.glow then
		button.glow = button:CreateTexture(nil, "OVERLAY")
		button.glow:SetTexture("Interface\\SpellActivationOverlay\\IconAlert")
		button.glow:SetPoint("CENTER", button)
		button.glow:SetHeight(button:GetHeight() * 1.3)
		button.glow:SetWidth(button:GetWidth() * 1.3)
		button.glow:SetTexCoord("0.00781250", "0.50781250", "0.53515625", "0.78515625")
		button.glow:SetVertexColor(1, 0, 0, 0.5)
		button.glow:SetBlendMode("ADD")
		button.glow:Hide()
	end
--]]
	self.styled = true
end

local caelButtons_Hotkey = function(self, actionButtonType)
	local hotkey = _G[format("%sHotKey", self:GetName())]

	if hotkey and hideHotkeys then
		hotkey:Hide()
	end
end

local caelButtons_Action = function(self)
	StyleButton(self)
end

local caelButtons_Pet = function()
	for i = 1, NUM_PET_ACTION_SLOTS do
		local name = format("PetActionButton%d", i)
		local button  = _G[name]
		local autocast = _G[format("%sAutoCastable", name)]

		autocast:SetAlpha(0)

		StyleButton(button)
	end
end

local caelButtons_Shapeshift = function()    
	for i = 1, NUM_STANCE_SLOTS do
		local name = format("StanceButton%d", i)
		local button  = _G[name]

		StyleButton(button)
	end
end

local caelButtons_ActionUsable = function(self)
	local name = self:GetName()
	local action = self.action
	local icon = _G[format("%sIcon", name)]
	local texture  = _G[format("%sNormalTexture", name)]

	if IsEquippedAction(action) then
		if texture then
			texture:SetVertexColor(0.33, 0.59, 0.33, 1)

			if self.gloss then
				self.gloss:SetBackdropColor(0.33, 0.59, 0.33, 0.5)
			end
		end
	else
		if texture then
			texture:SetVertexColor(0.5, 0.5, 0.5, 1)

			if self.gloss then
				self.gloss:SetBackdropColor(0.25, 0.25, 0.25, 0.5)
			end
		end
	end

	local isUsable, notEnoughPower = IsUsableAction(action)

	if ActionHasRange(action) and IsActionInRange(action) == 0 then
		icon:SetVertexColor(0.69, 0.31, 0.31)
		return
	elseif notEnoughPower then
		icon:SetVertexColor(0.31, 0.45, 0.63)
		return
	elseif isUsable then
		icon:SetVertexColor(1, 1, 1)
		return
	else
		icon:SetVertexColor(0.1, 0.1, 0.1)
		return
	end
end

local caelButtons_FixGrid = function(self)
	local texture  = _G[format("%sNormalTexture", self:GetName())]

	if texture then
		if IsEquippedAction(self.action) then
			texture:SetVertexColor(0.33, 0.59, 0.33, 1)
		else
			texture:SetVertexColor(0.5, 0.5, 0.5, 1)
		end
	end
end

local caelButtons_OnUpdate = function(self, elapsed)
	local time = self.cAB_range

	if (not time) then
		self.cAB_range = 0
		return
	end

	time = time + elapsed

	if (time < TOOLTIP_UPDATE_TIME + 0.1) then
		self.cAB_range = time
		return
	else
		self.cAB_range = 0
		caelButtons_ActionUsable(self)
	end
end

ActionButton_OnUpdate = caelButtons_OnUpdate

hooksecurefunc("ActionButton_Update", caelButtons_Action)
hooksecurefunc("ActionButton_UpdateHotkeys", caelButtons_Hotkey)
hooksecurefunc("ActionButton_UpdateUsable", caelButtons_ActionUsable)

--hooksecurefunc("PetActionBar_Update", caelButtons_Pet)
hooksecurefunc("StanceBar_Update", caelButtons_Shapeshift)
--hooksecurefunc("StanceBar_UpdateState", caelButtons_Shapeshift)

hooksecurefunc("ActionButton_ShowGrid", caelButtons_FixGrid)

--[[	Enable glowing overlays on macros.	]]

local overlayedSpells = {}

hooksecurefunc("ActionButton_HideOverlayGlow", function(button)
	if button.__LAB_Version then return end
	local actionType, id = GetActionInfo(button.action)

	if actionType == "macro" and overlayedSpells[GetMacroSpell(id) or false] then
		return ActionButton_ShowOverlayGlow(button)
	end
end)

hooksecurefunc("ActionButton_OnEvent",  function(button, event, spellId)
	if event == "SPELL_ACTIVATION_OVERLAY_GLOW_SHOW" or event == "SPELL_ACTIVATION_OVERLAY_GLOW_HIDE" then
		local spellName = GetSpellInfo(spellId)
		local actionType, id = GetActionInfo(button.action)
		local glowVisible = event == "SPELL_ACTIVATION_OVERLAY_GLOW_SHOW"

		overlayedSpells[spellName] = glowVisible

		if actionType == "macro" and GetMacroSpell(id) == spellName then
			if glowVisible then
				return ActionButton_ShowOverlayGlow(button)
			else
				return ActionButton_HideOverlayGlow(button)
			end
		end
	end
end)